package hydrap2p.speedlimiter;

import hydrap2p.helper.Utility;
import hydrap2p.logging.Level;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import org.dtools.ini.AdvancedIniFile;
import org.dtools.ini.AdvancedIniSection;
import org.dtools.ini.IniFile;
import org.dtools.ini.IniFileReader;
import org.dtools.ini.IniFileWriter;
import org.dtools.ini.IniItem;
import org.dtools.ini.IniSection;

public class Configuration {

	private Map<String, Integer> configs;
	
	public Configuration()
	{
		configs=new TreeMap<String, Integer>();
	}
	
	private boolean loadINI(Map<String, Integer> configs, File inifile)
	{
		if (!inifile.exists() || !inifile.isFile())
			return false;
		IniFile conf = new AdvancedIniFile();
		try {
			new IniFileReader(conf, inifile).read();
		} catch (IOException e) {
			return false;
		}

		IniSection section=conf.getSection("limits");
		if (section!=null)
		{
			for (IniItem item:section.getItems())
			{
				String name=item.getName();
				String value=item.getValue();
				int intvalue;
				try {
					intvalue=Integer.valueOf(value);
				}
				catch (NumberFormatException e)
				{
					// Could not parse the number
					SpeedLimiterLogging.log(Level.MISHAP, "Speed Limiter - Load Config", "Could not parse configuration in "+inifile.getAbsolutePath()+". Could not parse key "+name+" with value "+value);
					continue;
				}
				configs.put(name, intvalue);
			}
		}
		else
			return false;
		
		return true;
	}
	
	public boolean loadConfigs()
	{
		boolean ret=false;
		String path;
		
		configs=new TreeMap<String, Integer>();
		
		path=Utility.appendPath(Utility.getSharedConfigurationPath(), "speedlimiter.ini");
		ret|=loadINI(configs, new File(path));
		path=Utility.appendPath(Utility.getUserConfigurationPath(), "speedlimiter.ini");
		ret|=loadINI(configs, new File(path));
		
		return ret;
	}
	
	public int loadQueue(String queuename)
	{
		Integer ret=configs.get(queuename);
		if (ret==null)
			return 0;
		return ret;
	}
	
	private boolean saveINI(Map<String, Integer> configs, File inifile)
	{
		if (!inifile.exists() || !inifile.isFile())
			return false;
		IniFile conf = new AdvancedIniFile();
		
		IniSection section=new AdvancedIniSection("limits");
		for (Entry<String, Integer> entry:configs.entrySet())
		{
			IniItem item=new IniItem(entry.getKey());
			item.setValue(entry.getValue());
			section.addItem(item);
		}
		conf.addSection(section);
		try {
			new IniFileWriter(conf, inifile).write();
		} catch (IOException e) {
			return false;
		}

		return true;
	}
	
	public void saveQueue(String queuename, int value)
	{
		Map<String, Integer> userconfig=new TreeMap<String, Integer>();

		String path=Utility.appendPath(Utility.getUserConfigurationPath(), "speedlimiter.ini");
		File location=new File(path);
		loadINI(userconfig, new File(path));
		
		if (!location.exists())
		{
			try {
				location.createNewFile();
			} catch (IOException e) {
				SpeedLimiterLogging.log(Level.MISHAP, "Speed Limiter - Save Config", "Could not create file "+location.getAbsolutePath());
				e.printStackTrace();
				return;
			}
		}
		configs.put(queuename, value);
		userconfig.put(queuename, value);
		saveINI(userconfig, location);
	}
}
